/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsfcompref.model;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.xml.datatype.DatatypeConfigurationException;
import webservices.Cliente;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.WebServiceRef;
import javax.annotation.Resource;
import webservices.AgenciaWS_Service;
import webservices.AgenciaWS;

import javax.xml.datatype.XMLGregorianCalendar;
import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;


@ManagedBean
@RequestScoped
public class LoginBacking {
    
      @WebServiceRef(wsdlLocation = "http://190.17.0.161:8080/AgenciaWS/AgenciaWS?wsdl")
      public AgenciaWS_Service service;
      
      
      @Resource
      protected WebServiceContext context;
    
    private HtmlInputText userTextBox;
    private HtmlInputSecret passTextBox;
    private webservices.Cliente cli;
    
    public LoginBacking() throws DatatypeConfigurationException
    {
    
        cli = new Cliente();
        
        GregorianCalendar gcal2 = new GregorianCalendar();
	XMLGregorianCalendar fecnac = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal2);
	    
        
        cli.setFechaNacimiento(fecnac);
        
    }

    public Cliente getCli() {
        return cli;
    }

    public void setCli(Cliente cli) {
        this.cli = cli;
    }
    
      
    
    public HtmlInputSecret getPassTextBox() {
        return passTextBox;
    }

    public void setPassTextBox(HtmlInputSecret passTextBox) {
        this.passTextBox = passTextBox;
    }

    public HtmlInputText getUserTextBox() {
        return userTextBox;
    }

    public void setUserTextBox(HtmlInputText userTextBox) {
        this.userTextBox = userTextBox;
    }
        
    public String logIn() {
    
        String usr = userTextBox.getValue().toString();
        String pas = passTextBox.getValue().toString();
        
        AgenciaWS port = service.getAgenciaWSPort();
        Cliente clint = port.logInCliente(usr, pas);
        
        
        if (clint != null)
        {
            FacesContext cont = FacesContext.getCurrentInstance();
            ELContext elContext = cont.getELContext();
            Application application = cont.getApplication();
            
            ExpressionFactory expressionFactory = application.getExpressionFactory();
            ValueExpression ve = expressionFactory.createValueExpression(elContext, "#{uBean.cliente}",webservices.Cliente.class);
            
            ve.setValue(elContext, clint);
                    
            return "inicio";}
        else
        {return "faillogin";}
    }
    
    public String crearCliente() {
    
          AgenciaWS port = service.getAgenciaWSPort();
          
          port.createCliente(cli);
        
          return "done";
    
    }
        
}
